package com.wtgroup.demo.leetcode.q146_LRU缓存机制;

import javax.lang.model.element.VariableElement;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author dafei
 * @version 0.1
 * @date 2021/4/27 10:31
 */
public class Q146_Test {




}

/**
 * LinkedHashMap
 */
class LRUCache extends LinkedHashMap<Integer, Integer> {
    private int capacity;

    public LRUCache(int capacity) {
        // 这是关键 accessOrder
        super(capacity, 0.75F, true);
        this.capacity = capacity;
    }

    public int get(int key) {
        return super.getOrDefault(key, -1);
    }

    public void put(int key, int value) {
        super.put(key, value);
    }

    @Override
    protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
        return size() > capacity;
    }
}

